package com.shopmall.controller.system;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.GetResponse;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import com.alibaba.fastjson.JSON;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/test")
public class TestMqController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    ElasticsearchClient elasticsearchClient;

    @GetMapping("/send")
    public String test(){
        String messageId = UUID.randomUUID().toString();
        Map<String,String> map = new HashMap<>();
        map.put("messageId",messageId);
        map.put("userName","123456");
        map.put("password","111111");
        map.put("createTime",System.currentTimeMillis()+"");
        String messageData = JSON.toJSONString(map);
        rabbitTemplate.convertAndSend("76-exchanges","routingKey",messageData);
        return "success";
    }
    @GetMapping("/esadd")
    public String esAdd() throws IOException {
        Map<String,String> map = new HashMap<>();
        map.put("id","3112");
        map.put("name","张三2");
        map.put("sex","男2");
        map.put("desc","张三是一名会修理水电的技术工");
//        elasticsearchClient.indices().create(c->c.index("emp"));
        IndexResponse indexResponse = elasticsearchClient.index(i->i.index("emp").id(map.get("id")).document(map));
        System.out.println(indexResponse);
        GetResponse<Map> emp = elasticsearchClient.get(g -> g.index("emp").id("1"), Map.class);
        System.out.println(emp.source());
        return indexResponse.result().jsonValue();
    }

}
